package com.znz.service;

import com.znz.domain.Torder;
import com.znz.domain.Treturnvisit;
import com.znz.util.common.Page;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: duyuxin
 * Date: 15-4-28
 * Time: 下午9:59
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DashboardService {

    //TODO 查看当前客服的所有订单信息，如果是管理员则查看所有订单信息
    /**
     * @param userPhone 用户联系方式
     * @param userId   用户ID
     * @return
     */
    public Page<Torder> getAllOrderList(String userPhone,String userId,Page<Torder> page){
        StringBuilder where=new StringBuilder();
        List<Object> params=new ArrayList<Object>();
        try{
            if (null!=userPhone){
                 where.append(" and user_phone=? ");
                 params.add(userPhone);
            }
            if(null!=userId){
                where.append(" and c_user_id=? ");
                params.add(userId);
            }
            page.setTotal((int) Torder.countTorders());
            List<Torder> torderList=Torder.getTorderAllByWhere(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
            page.setRows(torderList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return page;
    }
    //TODO 根据当前客服id获取当月（自然月）业绩总额，如果是超级管理员则全部客服的业绩总额
    /**
     * @param userId 用户ID
     * @return
     */
    public double getAchievement(String userId){
        double  achievement=0;
        StringBuilder where=new StringBuilder();
        List<Object> params=new ArrayList<Object>();
        try{
            if(userId!=null){
               where.append(" and t3.id=? ");
               params.add(userId);
            }
            //TODO 获取当前月的第一天和最后一天
            Calendar cfirst = Calendar.getInstance();
            cfirst.add(Calendar.MONTH,0);
            cfirst.set(Calendar.DAY_OF_MONTH,1);
            Calendar clast = Calendar.getInstance();
            clast.set(Calendar.DAY_OF_MONTH, clast.getActualMaximum(Calendar.DAY_OF_MONTH));
            where.append(" and (expire_date<DATE_FORMAT(?, 'MM-dd-yyyy') and expire_date>DATE_FORMAT(?, 'MM-dd-yyyy'))");
            params.add(cfirst.getTime());
            params.add(clast.getTime());
            achievement=Treturnvisit.getAchievement(where.toString(),params);
            return achievement;
        }catch (Exception e){
           e.printStackTrace();
        }
        return achievement;
    }
    //TODO 根据当前客服id获取分配给自己的美丽需求数量
    /**
     * @param userId 用户ID
     * @return
     */
    public JSONObject getDesireCount(String userId){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("desire",10);
        return jsonObject;
    }
    //TODO 根据当前客服id获取当天待回访的数量
    /**
     * @param userId  用户ID
     * @return
     */
    public Long getReturnVisitCount(String userId){
        Long count=0l;
        try{
            if(userId!=null){
                count=Treturnvisit.getReturnVisitCount(userId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
}
